#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
调试评测功能
"""

import subprocess
import tempfile
import os
import time

def debug_python_execution():
    """调试Python代码执行"""
    print("=== 调试Python代码执行 ===")
    
    # 简单的代码
    code = 'print("Hello, World!")'
    
    # 创建临时文件
    temp_dir = tempfile.mkdtemp()
    source_file = os.path.join(temp_dir, 'test.py')
    
    try:
        with open(source_file, 'w', encoding='utf-8') as f:
            f.write(code)
        
        print(f"临时文件: {source_file}")
        print(f"代码内容: {code}")
        
        # 尝试执行
        start_time = time.time()
        try:
            result = subprocess.run(
                ['python', source_file],
                capture_output=True,
                text=True,
                timeout=5
            )
            execution_time = (time.time() - start_time) * 1000
            
            print(f"执行时间: {execution_time:.2f}ms")
            print(f"返回码: {result.returncode}")
            print(f"标准输出: '{result.stdout}'")
            print(f"标准错误: '{result.stderr}'")
            
        except subprocess.TimeoutExpired:
            print("执行超时")
        except Exception as e:
            print(f"执行异常: {e}")
            
    finally:
        # 清理
        try:
            import shutil
            shutil.rmtree(temp_dir, ignore_errors=True)
        except:
            pass

def debug_input_output():
    """调试输入输出"""
    print("\n=== 调试输入输出 ===")
    
    code = """
name = input()
print(f"Hello, {name}!")
"""
    
    temp_dir = tempfile.mkdtemp()
    source_file = os.path.join(temp_dir, 'test.py')
    
    try:
        with open(source_file, 'w', encoding='utf-8') as f:
            f.write(code)
        
        print(f"临时文件: {source_file}")
        print(f"代码内容: {code}")
        
        start_time = time.time()
        try:
            result = subprocess.run(
                ['python', source_file],
                input="World",
                capture_output=True,
                text=True,
                timeout=5
            )
            execution_time = (time.time() - start_time) * 1000
            
            print(f"执行时间: {execution_time:.2f}ms")
            print(f"返回码: {result.returncode}")
            print(f"标准输出: '{result.stdout}'")
            print(f"标准错误: '{result.stderr}'")
            
        except subprocess.TimeoutExpired:
            print("执行超时")
        except Exception as e:
            print(f"执行异常: {e}")
            
    finally:
        try:
            import shutil
            shutil.rmtree(temp_dir, ignore_errors=True)
        except:
            pass

def debug_python_command():
    """调试Python命令"""
    print("\n=== 调试Python命令 ===")
    
    try:
        # 检查Python版本
        result = subprocess.run(['python', '--version'], capture_output=True, text=True)
        print(f"Python版本: {result.stdout}")
        
        # 检查Python路径
        result = subprocess.run(['where', 'python'], capture_output=True, text=True)
        print(f"Python路径: {result.stdout}")
        
    except Exception as e:
        print(f"检查Python命令异常: {e}")

if __name__ == '__main__':
    debug_python_command()
    debug_python_execution()
    debug_input_output()
